//
//  ZGLBeiShuPlusOrMinus.m
//  ZeegingLottery
//
//  Created by Alfa Li on 2018/6/12.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLBeiShuPlusOrMinus.h"

@interface ZGLBeiShuPlusOrMinus ()<UITextFieldDelegate>


@property (nonatomic, strong)UILabel *beiShuLab;    //倍数
@property (nonatomic, strong)UIView *bottomView;

@end

@implementation ZGLBeiShuPlusOrMinus

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.bottomView];
        [self addSubview:self.beiShuLab];
        
        [self setupUI];
        
    }
    
    return self;
}

- (void)setupUI
{
    
    [_beiShuLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self);
        make.centerY.equalTo(self);
        
    }];
    
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
        make.left.equalTo(_beiShuLab.mas_right);
        
    }];
    
    
    [self setupBottomView];
}

- (void)setupBottomView
{
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = [UIColor grayColor];
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = [UIColor grayColor];
    
    [_bottomView addSubview:self.addBtn];
    [_bottomView addSubview:self.minusBtn];
//    [_bottomView addSubview:self.beiShuTF];
    [_bottomView addSubview:line2];
    [_bottomView addSubview:line1];
    [_bottomView addSubview:self.beiShuBtn];
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_bottomView);
        make.right.equalTo(_bottomView);
        make.bottom.equalTo(_bottomView);
        make.width.equalTo(@(25));
        
    }];
    
    [_minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_bottomView);
        make.centerY.equalTo(_addBtn);
        make.size.equalTo(_addBtn);
        
    }];
    
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_addBtn.mas_left);
//        make.height.equalTo(_addBtn);
        make.bottom.equalTo(_addBtn);
        make.top.equalTo(_addBtn);
        make.width.equalTo(@(kBorderWidth));
        
    }];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_minusBtn.mas_right);
        //        make.height.equalTo(line1);
        make.bottom.equalTo(line1);
        make.top.equalTo(line1);
        make.width.equalTo(line1);
        
    }];
    
    [_beiShuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(line1.mas_left);
        make.left.equalTo(line2.mas_right);
        make.height.equalTo(_addBtn);
        make.top.equalTo(_addBtn);
        make.width.equalTo(@(45));
        
    }];
    
}

- (void)setBeiShuNum:(NSInteger)beiShuNum
{
    _beiShuNum = beiShuNum;
    [self.beiShuBtn setTitle:[@(beiShuNum) stringValue] forState:UIControlStateNormal];
}


#pragma mark - getter


- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.layer.borderWidth = kBorderWidth;
        _bottomView.layer.borderColor = [UIColor grayColor].CGColor;
        _bottomView.layer.cornerRadius = 3;
        _bottomView.layer.masksToBounds = YES;

    }
    
    return _bottomView;
}


- (UIButton *)addBtn
{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"btn_custom_plus_minus_input_plus"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(jiaBeiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

- (UIButton *)minusBtn
{
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_minusBtn setBackgroundImage:[UIImage imageNamed:@"btn_custom_plus_minus_input_minus"] forState:UIControlStateNormal];
        [_minusBtn addTarget:self action:@selector(jianBeiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _minusBtn;
}

- (UIButton *)beiShuBtn
{
    if (!_beiShuBtn) {
        _beiShuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_beiShuBtn setTitle:@"10" forState:UIControlStateNormal];
        [_beiShuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _beiShuBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        [_beiShuBtn setBackgroundColor:[UIColor whiteColor]];
        [_beiShuBtn addTarget:self action:@selector(changeTheMultiples:) forControlEvents:UIControlEventTouchUpInside];
        _beiShuNum = [_beiShuBtn.titleLabel.text integerValue];
    }
    
    return _beiShuBtn;
}


- (UILabel *)beiShuLab
{
    if (!_beiShuLab) {
        _beiShuLab = [[UILabel alloc]init];
        _beiShuLab.text = @"倍数：";
        _beiShuLab.textAlignment = 1;
        _beiShuLab.textColor = detailTextColor;
        _beiShuLab.font = [UIFont systemFontOfSize:14];
    }
    return _beiShuLab;
}


#pragma  mark - BtnClick

- (void)changeTheMultiples:(UIButton *)sender
{
    if (self.beiShuNumBlock) {
        self.beiShuNumBlock(sender.titleLabel.text);
    }
}

//加
- (void)jiaBeiBtnClick:(UIButton *)sender
{
    DLog(@"加倍");
    if (self.beiShuNum < 1000) {
        
        self.beiShuNum += 1;
        
        [self.beiShuBtn setTitle:[@(self.beiShuNum) stringValue] forState:UIControlStateNormal];
    }
    
    if (self.beiShuNumBlock) {
        self.beiShuNumBlock([NSString stringWithFormat:@"%ld",self.beiShuNum]);
    }
    
}

//减
-(void)jianBeiBtnClick:(UIButton *)sender
{
    DLog(@"减倍");
    if (self.beiShuNum > 1) {
        self.beiShuNum -= 1;
        [self.beiShuBtn setTitle:[@(self.beiShuNum) stringValue] forState:UIControlStateNormal];
    }
    
    if (self.beiShuNumBlock) {
        self.beiShuNumBlock([NSString stringWithFormat:@"%ld",self.beiShuNum]);
    }
    
}

@end
